<?php

namespace App\Console\Commands;

use App\Library\Logic\AuctionOrderLogic;
use App\Library\Service\WxMessageService;
use App\Models\AuctionBidModel;
use App\Models\OpenIdModel;
use Illuminate\Console\Command;

class AuctionEndMessCommand extends Command
{
    /**
     * 拍卖结束已支付给所有出手人发信息
     *
     * @var string
     */
    protected $signature = 'command:auctionEndMess';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $list = AuctionOrderLogic::query()->where([
            'status_delete' => 0,
            'status_cancel' => 0,
            'status_pay' => 1,
            'is_push' => 0,
        ])->where([
            ['pay_at', '>', now()->subSeconds(5)->format('Y-m-d H:i:s')],
        ])->get();
        if (is_null($list)) return;
        foreach ($list as $val) {
            /*$bidUserIdArr = AuctionBidModel::query()->where('auction_id', $val['id'])
                ->pluck('user_id')->toArray();
            if (!empty($bidUserIdArr)) {
                $openList = OpenIdModel::query()
                    ->distinct()->whereIn('user_id', array_unique($bidUserIdArr))->get();
                if (!empty($openList)) {
                    foreach ($openList as $oVal) {
                        WxMessageService::auctionWxMess($val, $oVal);
                    }
                }
            }*/
            $val->is_push = 1;
            $val->save();
            $sellerOpenInfo = OpenIdModel::query()
                ->where('user_id', $val['seller_id'])->first();
            if (!empty($sellerOpenInfo)) {
                $sellerOpenInfo['address'] = json_decode($val['receive_info'],true)['ua_address_full'];
                WxMessageService::auctionWxMess($val, $sellerOpenInfo,2);
            }
        }
    }
}
